namespace TanmiaGrp.GeneralLedger.SqlServerRepository.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class Db001 : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Account",
                c => new
                    {
                        AccountID = c.Int(nullable: false, identity: true),
                        ParentAccountID = c.Int(),
                        FinalAccountID = c.Int(),
                        AccountNumber = c.String(nullable: false, maxLength: 64),
                        AccountName = c.String(nullable: false, maxLength: 128),
                        Level = c.Int(nullable: false),
                        IsTransactional = c.Boolean(nullable: false),
                        IsEnabled = c.Boolean(nullable: false),
                        IsDebitable = c.Boolean(nullable: false),
                        IsCreditable = c.Boolean(nullable: false),
                        IsCheckable = c.Boolean(nullable: false),
                        IsCashable = c.Boolean(nullable: false),
                        MaxDebit = c.Double(),
                        MaxCredit = c.Double(),
                        IsFinal = c.Boolean(nullable: false),
                        ExpiryDate = c.DateTime(),
                        TotalDebit = c.Double(nullable: false),
                        TotalCredit = c.Double(nullable: false),
                        Balance = c.Double(nullable: false),
                        LastOperationDate = c.DateTime(),
                        CreateDate = c.DateTime(nullable: false),
                        LastUpdateDate = c.DateTime(),
                        RowVersion = c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"),
                    })
                .PrimaryKey(t => t.AccountID)
                .ForeignKey("dbo.Account", t => t.FinalAccountID)
                .ForeignKey("dbo.Account", t => t.ParentAccountID)
                .Index(t => t.ParentAccountID)
                .Index(t => t.FinalAccountID)
                .Index(t => t.AccountNumber, unique: true, name: "IX_Account_AccountNumber")
                .Index(t => t.AccountName, unique: true, name: "IX_Account_AccountName");
            
            CreateTable(
                "dbo.Group",
                c => new
                    {
                        GroupID = c.Int(nullable: false, identity: true),
                        GroupName = c.String(nullable: false, maxLength: 64),
                        IsEnabled = c.Boolean(nullable: false),
                        Notes = c.String(maxLength: 255),
                        CreateDate = c.DateTime(nullable: false),
                        LastUpdateDate = c.DateTime(),
                        RowVersion = c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"),
                    })
                .PrimaryKey(t => t.GroupID);
            
            CreateTable(
                "dbo.Journal",
                c => new
                    {
                        JournalID = c.Int(nullable: false, identity: true),
                        PostState = c.Byte(nullable: false),
                        Notes = c.String(maxLength: 255),
                        TotalCredit = c.Double(nullable: false),
                        TotalDebit = c.Double(nullable: false),
                        Balance = c.Double(nullable: false),
                        CreateDate = c.DateTime(nullable: false),
                        LastUpdateDate = c.DateTime(),
                        RowVersion = c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"),
                    })
                .PrimaryKey(t => t.JournalID);
            
            CreateTable(
                "dbo.JournalEntry",
                c => new
                    {
                        JournalEntryID = c.Int(nullable: false, identity: true),
                        AccountID = c.Int(nullable: false),
                        JournalID = c.Int(nullable: false),
                        Credit = c.Double(nullable: false),
                        Debit = c.Double(nullable: false),
                        Description = c.String(nullable: false, maxLength: 255),
                        CreateDate = c.DateTime(nullable: false),
                        LastUpdateDate = c.DateTime(),
                        RowVersion = c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"),
                    })
                .PrimaryKey(t => t.JournalEntryID)
                .ForeignKey("dbo.Account", t => t.AccountID, cascadeDelete: false)
                .ForeignKey("dbo.Journal", t => t.JournalID, cascadeDelete: true)
                .Index(t => t.AccountID)
                .Index(t => t.JournalID);
            
            CreateTable(
                "dbo.Voucher",
                c => new
                    {
                        VoucherID = c.Int(nullable: false, identity: true),
                        Source = c.String(maxLength: 64),
                        SourceID = c.String(maxLength: 64),
                        Notes = c.String(maxLength: 255),
                        TotalCredit = c.Double(nullable: false),
                        TotalDebit = c.Double(nullable: false),
                        Balance = c.Double(nullable: false),
                        CreateDate = c.DateTime(nullable: false),
                        LastUpdateDate = c.DateTime(),
                        RowVersion = c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"),
                    })
                .PrimaryKey(t => t.VoucherID);
            
            CreateTable(
                "dbo.VoucherDetail",
                c => new
                    {
                        VoucherDetailID = c.Int(nullable: false, identity: true),
                        AccountID = c.Int(nullable: false),
                        VoucherID = c.Int(nullable: false),
                        Credit = c.Double(nullable: false),
                        Debit = c.Double(nullable: false),
                        Description = c.String(maxLength: 255),
                        CreateDate = c.DateTime(nullable: false),
                        LastUpdateDate = c.DateTime(),
                        RowVersion = c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion"),
                    })
                .PrimaryKey(t => t.VoucherDetailID)
                .ForeignKey("dbo.Account", t => t.AccountID, cascadeDelete: false)
                .ForeignKey("dbo.Voucher", t => t.VoucherID, cascadeDelete: true)
                .Index(t => t.AccountID)
                .Index(t => t.VoucherID);
            
            CreateTable(
                "dbo.AccountGroup",
                c => new
                    {
                        AccountID = c.Int(nullable: false),
                        GroupID = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.AccountID, t.GroupID })
                .ForeignKey("dbo.Account", t => t.AccountID, cascadeDelete: true)
                .ForeignKey("dbo.Group", t => t.GroupID, cascadeDelete: false)
                .Index(t => t.AccountID)
                .Index(t => t.GroupID);
            
        }
        
        public override void Down()
        {
            DropForeignKey("dbo.VoucherDetail", "VoucherID", "dbo.Voucher");
            DropForeignKey("dbo.VoucherDetail", "AccountID", "dbo.Account");
            DropForeignKey("dbo.JournalEntry", "JournalID", "dbo.Journal");
            DropForeignKey("dbo.JournalEntry", "AccountID", "dbo.Account");
            DropForeignKey("dbo.Account", "ParentAccountID", "dbo.Account");
            DropForeignKey("dbo.Account", "FinalAccountID", "dbo.Account");
            DropForeignKey("dbo.AccountGroup", "GroupID", "dbo.Group");
            DropForeignKey("dbo.AccountGroup", "AccountID", "dbo.Account");
            DropIndex("dbo.AccountGroup", new[] { "GroupID" });
            DropIndex("dbo.AccountGroup", new[] { "AccountID" });
            DropIndex("dbo.VoucherDetail", new[] { "VoucherID" });
            DropIndex("dbo.VoucherDetail", new[] { "AccountID" });
            DropIndex("dbo.JournalEntry", new[] { "JournalID" });
            DropIndex("dbo.JournalEntry", new[] { "AccountID" });
            DropIndex("dbo.Account", "IX_Account_AccountName");
            DropIndex("dbo.Account", "IX_Account_AccountNumber");
            DropIndex("dbo.Account", new[] { "FinalAccountID" });
            DropIndex("dbo.Account", new[] { "ParentAccountID" });
            DropTable("dbo.AccountGroup");
            DropTable("dbo.VoucherDetail");
            DropTable("dbo.Voucher");
            DropTable("dbo.JournalEntry");
            DropTable("dbo.Journal");
            DropTable("dbo.Group");
            DropTable("dbo.Account");
        }
    }
}
